package com.yonyou.brigade.third_parties.ctripBC.util;

import com.yonyou.brigade.basic_init.model.BriChannel;
import com.yonyou.brigade.third_parties.common.model.ThirdProperty;
import com.yonyou.brigade.third_parties.ctripBC.model.CtripBCUser;
import com.yonyou.brigade.util.operateutil.StringUtils;
import com.yonyou.ybzboot.uc.dto.UcUserDto;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class CtripBCUserConvertUtil {
    public static List<CtripBCUser> getCtripBCUser(List<UcUserDto> ucUserDtoList, String corporationID, String tenant, String orgpk, List<ThirdProperty> thirdProperties, BriChannel briChannel){
        List<CtripBCUser> ctripBCUsers=new ArrayList<>();
        for(UcUserDto ucUserDto:ucUserDtoList){
            CtripBCUser ctrip=getCtripBCUser(ucUserDto,corporationID,tenant,orgpk,thirdProperties, briChannel);
            ctripBCUsers.add(ctrip);
        }
        return ctripBCUsers;
    }


    /**
     * 用户中心的用户转化为写携程商旅通用户
     *
     * @param ucUserDto
     * @param corporationID
     * @return
     */
    public static CtripBCUser getCtripBCUser(UcUserDto ucUserDto, String corporationID, String tenant, String orgpk, List<ThirdProperty> thirdProperties, BriChannel briChannel) {
        CtripBCUser ctripBCUser = new CtripBCUser();
        ctripBCUser.setTenant(tenant);
        ctripBCUser.setOrg(ucUserDto.getOrgPk());
        ctripBCUser.setCorporationID(corporationID);
        ctripBCUser.setDept1(ucUserDto.getOrgName());
        ctripBCUser.setDept2(ucUserDto.getDeptName());
        ctripBCUser.setEmail(ucUserDto.getEmail());
        ctripBCUser.setEmployeeId(ucUserDto.getPersonCode());
        //邮箱仍然同步
        ctripBCUser.setEmail(ucUserDto.getEmail());
        ctripBCUser.setMobilePhone(ucUserDto.getPhone());
        ctripBCUser.setName(ucUserDto.getUserName());
        if (0 != briChannel.getSyncRank()) {
            ctripBCUser.setRank(ucUserDto.getPostPk());
            ctripBCUser.setRankName(ucUserDto.getPostName());
        }
        ctripBCUser.setUserId(ucUserDto.getUserId());
        ctripBCUser.setValid(String.valueOf(ucUserDto.getValid() == 1 ? "A" : "I"));
//        if (ctripBCUser.getEmail() != null) {
//            ctripBCUser.setIsSendEmail("true");
//        } else {
//            ctripBCUser.setIsSendEmail("false");
//        }
        //不发送邮件
        ctripBCUser.setIsSendEmail("false");
        /*加载其他的自定义属性 start*/
        if (thirdProperties != null && thirdProperties.size() > 0) {
            for (ThirdProperty thirdProperty : thirdProperties) {
                try {
                    Field f = ctripBCUser.getClass().getDeclaredField(StringUtils.lowerFirstChar(thirdProperty.getKey()));
                    f.setAccessible(true);
                    if (thirdProperty.getType().equals("INTEGER")) {
                        int value = Integer.parseInt(thirdProperty.getValue());
                        f.set(ctripBCUser, value);
                    } else {
                        f.set(ctripBCUser, thirdProperty.getValue());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    log.error(e.getMessage());
                }
            }
        }
        /*加载其他的自定义属性 end*/
        return ctripBCUser;
    }

}
